package com.intel.two.service.information.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.information.LostMapper;
import com.intel.two.pojo.information.Lost;
import com.intel.two.service.information.LostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("/LostService")
public class LostServiceImpl implements LostService {
    @Autowired
    private LostMapper lostMapper;

    @Override
    public LayuiPageVo<Lost> lostPageData(Map<String, Object> params) {

        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Lost> losts =  lostMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<Lost> lostPageInfo = new PageInfo<>(losts);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<Lost> lostLayuiPageVo = new LayuiPageVo<>();
        lostLayuiPageVo.setCode(0);
        lostLayuiPageVo.setMsg("学生分页列表数据");
        lostLayuiPageVo.setCount(lostPageInfo.getTotal());  // 总记录数
        lostLayuiPageVo.setData(lostPageInfo.getList());  // 分页列表数据

        return lostLayuiPageVo;
    }


    @Override
    public int deleteLost(Integer lost_id) {
        return lostMapper.deleteLost(lost_id);
    }

    @Override
    public int addLost(Lost lost) {
        return lostMapper.addLost(lost);
    }

    @Override
    public int updateLost(Lost lost) {
        return lostMapper.updateLost(lost);
    }

    @Override
    public int deleteLostAll(Map<String, Object> map) {
        String str = (String) map.get("lost_id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return lostMapper.deleteLostAll(list);
    }

}
